package jsyf.oa.dao.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.stereotype.Repository;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jsyf.oa.entity.device.DeviceModelEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * Created by jjx on 2018/3/8.
 */
@Repository
public class DeviceModelDao extends BaseDao {

    public DeviceModelDao() {
        super.iBaseEntity = new DeviceModelEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {

        return JSON.toJavaObject(json, DeviceModelEntity.class);
    }

    public List<JSONObject> getModelByBrandId(String brandId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" select distinct(t1.MODEL_NAME)  as modelName,(t1.ID) as modelId,(t1.LOWEST_PRICE) as lowestPrice");
        sb.append(" from t_dvc_model as t1 LEFT JOIN t_dvc_brand as t2 ");
        sb.append(" ON t1.BRAND_ID = t2.ID ");
        sb.append(" where t2.ID = ? and t1.DEL_FLAG=0 ");
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString(), new String[]{brandId});
        return jsonList;
    }

    public List<JSONObject> getBucketCapacityList(String brandId, String modelId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" select distinct( t1.BUCKET_CAPACITY) as bucketCapacity");
        sb.append(" from t_dvc_model as t1 LEFT JOIN t_dvc_brand as t2 ");
        sb.append(" ON t1.BRAND_ID = t2.ID ");
        sb.append("  where t2.id=? and t1.DEL_FLAG=0 and t1.Id =?  ");
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString(), new String[]{brandId, modelId});
        return jsonList;
    }

    public JSONObject getLow(String bucketCapacity, String modelId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" select distinct(LOWEST_PRICE) as lowestPrice  from t_dvc_model");
        sb.append("  where BUCKET_CAPACITY=? and DEL_FLAG=0 and Id =?  ");
        JSONObject jsonList = super.queryForJsonObject(sb.toString(), new String[]{bucketCapacity, modelId});
        return jsonList;
    }

    public List<JSONObject> getJSONListByPage1(String condition, Object[] params, int requestPage, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT ");
        sql.append(" T_DVC_MODEL.ID AS id, ");
        sql.append(" MODEL_NAME AS modelName, ");
        sql.append(" BUCKET_CAPACITY AS bucketCapacity, ");
        sql.append(" LOWEST_PRICE AS lowestPrice, ");
        sql.append(" BRAND_NAME AS brandName ");
        sql.append(" FROM ");
        sql.append(iBaseEntity.getTableName());
        sql.append(condition);
        super.appendPageSql(sql, requestPage, pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(), params);

        return jsonList;
    }

    public Integer getPageCount(String condition, Object[] params) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT COUNT(1) AS count FROM " + iBaseEntity.getTableName());
        sql.append(condition);
        String count = super.queryForString(sql.toString(), params);
        return Integer.parseInt(count);
    }

    public List<JSONObject> getBrandListBytypeId(String companyId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT");
        sql.append(" t_dvc_brand.ID AS id,");
        sql.append(" t_dvc_brand.BRAND_NAME as brandName");
        sql.append(" FROM `t_dvc_model` LEFT JOIN t_dvc_brand ON t_dvc_model.BRAND_ID=t_dvc_brand.ID");
        sql.append(" LEFT JOIN t_sys_company_2_brand ON t_dvc_model.BRAND_ID=t_sys_company_2_brand.BRAND_ID");
        sql.append(" WHERE t_dvc_brand.DEL_FLAG=0 and t_dvc_model.DEL_FLAG=0");
        sql.append(" AND t_sys_company_2_brand.COMPANY_ID=?");
        sql.append(" GROUP BY BRAND_NAME");
        return super.queryForJsonList(sql.toString(), new Object[]{companyId});
    }

    public List<JSONObject> getBrand() {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT");
        sql.append(" t_dvc_brand.ID AS id,");
        sql.append(" t_dvc_brand.BRAND_NAME as brandName");
        sql.append(" FROM `t_dvc_brand`");
        sql.append(" GROUP BY BRAND_NAME");
        return super.queryForJsonList(sql.toString(), new Object[]{});
    }

    public List<JSONObject> getModelListBybrandId(String brandId, String deviceTypeId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT");
        sql.append(" ID as id,");
        sql.append(" MODEL_NAME as modelName,");
        sql.append(" BRAND_ID as brandId,");
        sql.append(" BUCKET_CAPACITY as bucketCapacity,");
        sql.append(" CONFIG as config,");
        sql.append(" LOWEST_PRICE as lowestPrice");
        sql.append(" FROM `t_dvc_model` where t_dvc_model.BRAND_ID=? and t_dvc_model.DEVICE_TYPE_ID=?");
        sql.append(" AND t_dvc_model.DEL_FLAG=0");
        return super.queryForJsonList(sql.toString(), new Object[]{brandId, deviceTypeId});
    }

    public List<JSONObject> getModelListByBId(String brandId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT");
        sql.append(" t_dvc_model.ID as modelId,");
        sql.append(" t_dvc_model.MODEL_NAME as modelName,");
        sql.append(" t_dvc_brand.brand_NAME as brandName,");
        sql.append(" t_dvc_brand.QUALITY_COMPANY as qualityCompany,");
        sql.append(" t_dvc_brand.AUTHENTICBRANDS_GROUP as authenticbrandsGroup,");
        sql.append(" t_dvc_brand.FINANCE_COMPANY as financeCompany,");
        sql.append(" t_dvc_brand.MANUFACTURING_COMPANY as manufacturer,");
        sql.append(" t_dvc_device_type.name as deviceType");
        sql.append(" FROM `t_dvc_model`");
        sql.append(" left join t_dvc_brand on t_dvc_brand.id=t_dvc_model.brand_id");
        sql.append(" left join t_dvc_device_type on t_dvc_device_type.id=t_dvc_model.DEVICE_TYPE_ID");
        sql.append(" where t_dvc_model.BRAND_ID=?");
        sql.append(" AND t_dvc_model.DEL_FLAG=0");
        sql.append(" AND t_dvc_device_type.DEL_FLAG=0");
        return super.queryForJsonList(sql.toString(), new Object[]{brandId});
    }

    public List<JSONObject> getBrandByCompanyId(String companyId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT\n");
        sql.append(" t_dvc_brand.ID AS id,\n");
        sql.append(" t_dvc_brand.BRAND_NAME as brandName\n");
        sql.append(" FROM `t_dvc_brand`\n");
        sql.append(" LEFT JOIN t_sys_company_2_brand ON t_sys_company_2_brand.BRAND_ID=t_dvc_brand.ID\n");
        sql.append(" WHERE t_sys_company_2_brand.COMPANY_ID=?\n");
        sql.append(" GROUP BY BRAND_NAME\n");
        return super.queryForJsonList(sql.toString(), new Object[]{companyId});
    }
}
